package com.example.web;

import com.example.pojo.Cart;
import com.example.pojo.Order;
import com.example.pojo.OrderItem;
import com.example.pojo.User;
import com.example.service.OrderService;
import com.example.service.impl.OrderServiceImpl;
import com.example.utils.WebUtils;
import com.example.utils.jdbcUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet(name = "OrderServlet", value = "/OrderServlet")
public class OrderServlet extends BaseServlet {

    OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //先获取购物车对象cart
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //获取UserId
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null){
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            return;
        }
        Integer userId = loginUser.getId();
        String orderId = orderService.createOrder(cart, userId);

//        request.setAttribute("orderId", orderId);
        request.getSession().setAttribute("orderId", orderId);
        //请求转发到 /pages/cart/checkout.jsp   BUG
        //request.getRequestDispatcher("/pages/cart/checkout.jsp").forward(request, response);

        //重定向
        response.sendRedirect(request.getContextPath()+"/pages/cart/checkout.jsp");
    }

    /**
     * 列出所有订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void showAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Order> orders = orderService.showAllOrders();
        request.setAttribute("orders", orders);
        request.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(request, response);
    }

    /**
     * 发货
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        orderService.sendOrder(orderId);
        response.sendRedirect(request.getHeader("Referer"));
    }

    /**
     * 展示订单细节
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void showOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        Integer count = 0;
        BigDecimal totalPrice = BigDecimal.valueOf(0);
        for (OrderItem orderItem : orderItems) {
            count += orderItem.getCount();
            totalPrice = totalPrice.add(orderItem.getTotalPrice());
        }
        request.setAttribute("orderItems",orderItems);
        request.setAttribute("count",count);
        request.setAttribute("totalPrice",totalPrice);
        request.getRequestDispatcher("pages/order/orderItem.jsp").forward(request, response);
    }
    protected void showMyOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null){
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            return;
        }
        Integer userId = loginUser.getId();
        List<Order> orders = orderService.showMyOrders(userId);
        request.setAttribute("orders", orders);
        request.getRequestDispatcher("pages/order/order.jsp").forward(request, response);
    }


}
